package org.example.demo.bundle;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;

import java.util.Locale;
import java.util.ResourceBundle;

public class ResourceManager {
    private static ResourceManager instance;
    private ResourceBundle bundle;
    private final ObjectProperty<Locale> currentLocale = new SimpleObjectProperty<>();

    private ResourceManager() {
        // 初始化默认语言（如从配置文件读取）
        setLocale(Locale.ENGLISH);
    }

    public static ResourceManager getInstance() {
        if (instance == null) {
            instance = new ResourceManager();
        }
        return instance;
    }

    public void setLocale(Locale locale) {
        if (!locale.equals(currentLocale.get())) {
            bundle = ResourceBundle.getBundle("language", locale);
            currentLocale.set(locale);
        }
    }

    public String getString(String key) {
        return bundle.getString(key);
    }

    // 暴露 Observable 属性供外部绑定
    public ObjectProperty<Locale> currentLocaleProperty() {
        return currentLocale;
    }

    public ResourceBundle getBundle(){
        return bundle;
    }
}
